package com.linln.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil
{
  public static final Pattern FLOAT_PATTERN = Pattern.compile("^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$");
  public static final Pattern INTEGER_PATTERN = Pattern.compile("^-?[1-9]\\d*$");
  public static final Pattern UINTEGER_INZERO_PATTERN = Pattern.compile("^\\+?[0-9][0-9]*$");
  public static final Pattern UINTEGER_OUTZERO_PATTERN = Pattern.compile("^\\+?[1-9][0-9]*$");


  public static String decodeString(String str)    
  {
    String strReturn = "";    

    try {
		str = new String(str.getBytes("UTF-8"), "UTF-8");
		strReturn = URLDecoder.decode(str, "UTF-8");
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}    

    return strReturn;
  }

  public static String encodeString(String str)    
  {
    String strReturn = "";

    try {
		strReturn = URLEncoder.encode(str, "UTF-8");
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

    return strReturn;
  }

  public static boolean isEmpty(String string) {
		if(string!=null && "null".equals(string.toLowerCase().trim()))return true;
		return string == null || string.trim().length() == 0;
	}

  public static String nullIfEmpty(String string) {
		return isEmpty(string) ? null : string;
	}

  public static String emptyIfNull(String string) {
		return (string == null || "null".equals(string)) ? "" : string;
	}
  public static String trimString(String string){
		
		return string != null ? string.trim() : string;
	}
  
  public static boolean isUInteger(String strNumber, boolean isContainZero)
  {
    boolean blnUInteger = false;

    Matcher matcher = null;
    if (isContainZero)
      matcher = UINTEGER_INZERO_PATTERN.matcher(strNumber);
    else {
      matcher = UINTEGER_OUTZERO_PATTERN.matcher(strNumber);
    }

    blnUInteger = matcher.matches();

    return blnUInteger;
  }

  public static boolean isFloat(String strNumber)
  {
    boolean blnFloat = false;

    Matcher matcher = FLOAT_PATTERN.matcher(strNumber);
    blnFloat = matcher.matches();

    return blnFloat;
  }

  public static boolean isInteger(String strNumber)
  {
    boolean blnInt = false;

    Matcher matcher = INTEGER_PATTERN.matcher(strNumber);
    blnInt = matcher.matches();

    return blnInt;
  }
  
  /**   
   * 用来判断邮箱是否合法   
   * @param   email   String   
   * @return   boolean   
   */   
  public static boolean verifyEmail(String   email){   
 	 Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");

 	 Matcher matcher = pattern.matcher(email);

 	 return matcher.matches();   
   }
  
  
  public static String split(String value){
		if(value==null){
			return null;
		}
		StringBuffer result=new StringBuffer(value.length());
		for(int i=0;i<value.length();++i){
			switch(value.charAt(i)){
			case '|':
				result.append("");
				break;
			case '&':
				result.append("");
				break;
			case ';':
				result.append("");
				break;
			case '$':
				result.append("");
				break;
			case '%':
				result.append("");
				break;
			case '\'':
				result.append("");
				break;
			case '"':
				result.append("");
				break;
			case '\\':
				result.append("");
				break;
			case '<':
				result.append("");
				break;
			case '>':
				result.append("");
				break;
			case '+':
				result.append("");
				break;
			case '\r':
				result.append("");
				break;
			case '\n':
				result.append("");
				break;
			default:
				result.append(value.charAt(i));
				break;
			}
			
		}
		return result.toString();	
	}
  
}